package edu.imac.angryfood.component;

import javax.swing.Timer;

import org.jbox2d.common.Vec2;
import org.newdawn.slick.SlickException;

import edu.imac.angryfood.world.World;

/** A projectile that accelerates when the user clicks and it is still in the air */
public class AcceleratedProjectile extends AbstractProjectile {

	/** constructor for circle projectiles
	 * 
	 * @param x x position of the projectile
	 * @param y y position of the projectile
	 * @param radius radius of the projectile
	 * @param droped if the projectile has already been droped
	 * @param num the order in which the projectile will be launched
	 * @param image the name of the image's file
	 */
	
	public AcceleratedProjectile(int x, int y, float radius, boolean droped, int num, String image) throws SlickException {
		super(x, y, radius, droped, num, image);
	}
	
	/** constructor for rectangle projectiles
	 * 
	 * @param x x position of the projectile
	 * @param y y position of the projectile
	 * @param width width of the projectile
	 * @param height height of the projectile
	 * @param droped if the projectile has already been droped
	 * @param num the order in which the projectile will be launched
	 * @param image the name of the image's file
	 */
	
	public AcceleratedProjectile(int x, int y, int width, int height, boolean droped, int num, String image) throws SlickException {
		super(x, y, width, height, droped, num, image);
	}

	/** when the user clicks, the projectile accelerates
	 * 
	 * @param world the world that contains the game
	 * @throws SlickException
	 */
	
	@Override
	public void click(World world) throws SlickException {
		if (tLaunched instanceof Timer){
			tLaunched.stop();
		}
		clicked=true;
		next(world);
		Vec2 velocity=getLinearVelocity();
		float norm=(float) (Math.sqrt(Math.pow(velocity.x, 2)+Math.pow(velocity.y, 2)));
		//the velocity is normalized
		velocity.x/=norm;
		velocity.y/=norm;
		//and then multiplied to keep the same direction
		velocity.x*=80;
		velocity.y*=80;
		getJBoxBody().setLinearVelocity(velocity);
	}

	/** when the projectile collides something, it cannot be clicked anymore
	 * 
	 * @param world the world that contains the game
	 */
	@Override
	public void collision(World world) {	
		if (!collided){
			collided=true;
			clicked=true;		
			next(world);
			if (tLaunched instanceof Timer){
				tLaunched.stop();
			}
		}
	}

}
